﻿using FlowControl;
using System.Collections;
using UnityEngine;

public class AllUpdate : MonoBehaviour
{
    public int webPort = 35000;
    public string websvrHost = "127.0.0.1";
    private FlowContainerLine doMainLine;

    public static GameObject Camer2D { get; private set; }
    public static GameObject PixelGo { get; private set; }
    public static GameObject UIRoot { get; private set; }

    public static GameObject ConsoleLike { get; private set; }
    public static PlatFormMgr PlatFormMgr { get; private set; }
    public static NotifyMessage NotifyMessage { get; private set; }

    private void Awake()
    {
        UIRoot = gameObject; DontDestroyOnLoad(gameObject);
        PixelGo = GetChildByName("PixelResolu").gameObject;
        Camer2D = GetChildByName("Camera").gameObject;

        ConsoleLike = GetChildByName("ConsoleLike").gameObject;
        PlatFormMgr = ConsoleLike.GetComponent<PlatFormMgr>();
        NotifyMessage = ConsoleLike.GetComponent<NotifyMessage>();
    }

    private void Start()
    {
        // 打开客户端的时间点，先连接websvr
        WebClient.GetSingleton().InitData(websvrHost, webPort);
        if (WebClient.GetSingleton().ConnectWeb())
        {
            StartLoad(null);
        }
        else
        {
            ZEventCenter.AddEventListener(ConstData.WEBMGR_CONNECT_OK, StartLoad);
        }
    }

    private void Update ()
    {
        if (doMainLine != null && !WebClient.GetSingleton().Exception)
        {
            doMainLine.FlowUpdate(Time.realtimeSinceStartup);
            UpdateMgr.GetSingleton().RemoveUpdate();
            UpdateMgr.GetSingleton().RunUpdate();
        }
	}

    private void StartLoad(ZEvent ev)
    {
        BehaviourUtil.StartCoroutine(StartFlow());
    }

    private IEnumerator StartFlow () 
    {
        ZEventCenter.RemoveEventListener(ConstData.WEBMGR_CONNECT_OK, StartLoad);
        UserPrefsMgr.deviceId = SystemInfo.deviceUniqueIdentifier;
        //解决视频播放完锁屏的问题
        Screen.sleepTimeout = SleepTimeout.NeverSleep;     
        //调整分辨率挪到这里
        yield return BehaviourUtil.StartCoroutine(ModResolution());
        Handheld.PlayFullScreenMovie("introduce.mp4", Color.black, FullScreenMovieControlMode.CancelOnInput);
        
#if DEBUG
        gameObject.AddComponent<OutLog>();
        gameObject.AddComponent<FPSCounter>();
        EDebug.enabled = true;
#else
        EDebug.enabled = false;
#endif
        
        doMainLine = new FlowContainerLine();
        doMainLine.Name = "流程控制主线";

        FlowCheckUpdate checkUpdate = new FlowCheckUpdate();
        checkUpdate.Name = "检测更新";

        FlowLoadCFG loadCFG = new FlowLoadCFG();
        loadCFG.Name = "加载主配置文件";

        FlowLoadLuaBundle loadLua = new FlowLoadLuaBundle();
        loadLua.Name = "加载Lua脚本文件";

        FlowReadCFG readCFG = new FlowReadCFG();
        readCFG.Name = "解析数据配置";

        FlowGameInit gameInit = new FlowGameInit();
        gameInit.Name = "游戏逻辑初始化";

        FlowLoadInitAsset loadInitAsset = new FlowLoadInitAsset();
        loadInitAsset.Name = "加载初始化资源";

        doMainLine.AddFlow(checkUpdate);
        doMainLine.AddFlow(loadCFG);
        doMainLine.AddFlow(loadLua);
        doMainLine.AddFlow(readCFG);
        doMainLine.AddFlow(gameInit);
        
        doMainLine.AddFlow(loadInitAsset);
        doMainLine.Activate();
        yield break;
	}

    private IEnumerator ModResolution()
    {
        Utility.SetActive(PixelGo, true);
        yield return null;

        int phyWidth = Screen.currentResolution.width;
        int phyHeight = Screen.currentResolution.height;
        int highWidth = GlobalNum.HIGH_HEIGHT * phyWidth / phyHeight;
        Screen.SetResolution(highWidth, GlobalNum.HIGH_HEIGHT, true);

        yield return new WaitForEndOfFrame();
        Utility.SetActive(PixelGo, false);
    }
    
    public static Transform GetChildByName(string path)
    {
        return UIRoot.transform.Find(path);
    }
}